package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    /**
     * 根据条件查询频道列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {

        //1.参数校验
        if (dto==null){
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //检查分页
        dto.checkParam();
        //2.构建查询条件执行查询
        //分页
        IPage<AdChannel> pageParam=new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdChannel> query=new LambdaQueryWrapper<>();

        //构建查询条件
        //频道模糊查询
        if (StringUtils.isNotBlank(dto.getName())){
            query.like(AdChannel::getName,dto.getName());
        }
        if (dto.getStatus()!=null){
            query.eq(AdChannel::getStatus,dto.getStatus());
        }

        IPage<AdChannel> pageResult = page(pageParam, query);
        //3.封装返回结果
        PageResponseResult result= new PageResponseResult(dto.getPage(),dto.getSize(),
                pageResult.getTotal(),pageResult.getRecords());
        return result;
    }

    /**
     * 新增
     * @param adChannel
     * 优化：前端添加是否默认
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel adChannel) {
        // 1 参数校验
        if (adChannel == null || StringUtils.isBlank(adChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        // 2 执行新增

        adChannel.setCreatedTime(new Date());

        if (adChannel.getIsDefault()) {  // 是默认
            // 查询原有的默认的频道，更新为非默认
            LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AdChannel::getIsDefault, 1);
            AdChannel one = getOne(wrapper);

            // 判断是否存在默认频道
            if (one != null) {
                // 更新为非默认
                one.setIsDefault(false);
                updateById(one);
            }
        }

        save(adChannel);
        // 3 返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(AdChannel adChannel) {

        //1.检查参数
        if(null == adChannel || adChannel.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 先查后改
        AdChannel channel = getById(adChannel.getId());
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //2.修改
        updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        //1.检查参数
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断当前频道是否存在或者有效
        AdChannel adChannel = getById(id);
        if (adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }//默认频道
        if (adChannel.getIsDefault()){
            ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_ISDEFAULT_INVALID);
        }// 启用状态下不能删除
        if (adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMIN_CHANNEL_ISSTATUS_ERROR);
        }
        //3.删除频道
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
